// 导入数据库API
const db = require("../../config/db");

module.exports = {
    add_cart(cart_id, gid, goods_img, goods_name, goods_price, goods_color, goods_size, goods_num) {
        return new Promise((resolve, reject) => {
            let cart_insert_sql = `insert into db_cart(cart_id,gid,goods_img,goods_name,goods_price,goods_color,goods_size,goods_num) values("${cart_id}","${gid}","${goods_img}","${goods_name}","${goods_price}","${goods_color}","${goods_size}","${goods_num}")`;
            db.query(cart_insert_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },

    update_cart(cart_id, goods_color, goods_size, goods_num) {
        return new Promise((resolve, reject) => {
            let update_cart_sql = `update db_cart set goods_color="${goods_color}",goods_size="${goods_size}",goods_num="${goods_num}" where cart_id="${cart_id}"`;
            db.query(update_cart_sql, (err, result) => {
                if (err) {
                    reject(err);
                } else {
                    resolve({
                        code: 200,
                        msg: "修改成功",
                    });
                }
            });
        });
    },

    get_cart() {
        return new Promise((resolve, reject) => {
            let get_cart_sql = `select * from db_cart`;
            db.query(get_cart_sql, (err, result) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(result);
                }
            });
        });
    },

    remove_cart(gids) {
        return new Promise((resolve, reject) => {
            let remove_cart_sql = `DELETE FROM db_cart WHERE gid IN (${gids});`;
            db.query(remove_cart_sql, (err, result) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(result);
                }
            });
        });
    },
};
